//
//  RealmViewController.swift
//  DemoProject
//
//  Created by Hao on 2019/5/22.
//  Copyright © 2019 hao. All rights reserved.
//

import UIKit
import RealmSwift

class RealmViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupRealm()
    }
    
    private func setupRealm() {
        // Document目录后不能加二级目录。/Realm/data.realm 就会报错‘data.realm.management': make_dir() failed: No such file or directory Path:.’，
        let realmPath = (kDocumentDirectory as NSString).appendingPathComponent("/data.realm")
        let configuration = Realm.Configuration(fileURL: URL(fileURLWithPath: realmPath))
        
        let realm = try! Realm(configuration: configuration)
        // 获取 Realm 文件的父目录
        let folderPath = realm.configuration.fileURL!.deletingLastPathComponent().path
        // 禁用此目录的文件保护
        try! FileManager.default.setAttributes([FileAttributeKey.protectionKey : FileProtectionType.none],
                                               ofItemAtPath : folderPath)
        print(info: "folderPath -> \(folderPath)")
    }

}
